<?php
$message_id = isset( $_GET['message_id'] ) ? $_GET['message_id'] : 0;
$message_parent = isset( $_GET['message_parent'] ) ? $_GET['message_parent'] : -1;
$forum_id = dPgetParam($_REQUEST, 'forum_id', 0);

//Pull forum information
$q  = new DBQuery;
$q->addTable('forums');
$q->addTable('projects');
$q->addQuery('forum_name, forum_owner, forum_moderated, project_name, project_id');
$q->addWhere("forums.forum_id = $forum_id");
$q->addWhere('forums.forum_project = projects.project_id');
$res = $q->exec();
$forum_info = $q->fetchRow();
$q->clear();
echo db_error();

//pull message information
$q  = new DBQuery;
$q->addTable('forum_messages');
$q->addQuery('forum_messages.*, user_username');
$q->addJoin('users', 'u', 'message_author = u.user_id');
$q->addWhere('message_id = '. ($message_id ? $message_id : $message_parent));
$res = $q->exec();
echo db_error();
$message_info = $q->fetchRow();
$q->clear();

//pull message information from last response 
if ($message_parent != -1)
{
	$q->addTable('forum_messages');
	$q->addWhere('message_parent = '. ($message_id ? $message_id : $message_parent));
	$q->addOrder('message_id DESC'); // fetch last message first
	$q->setLimit(1);
	$res = $q->exec();
	echo db_error();
	$last_message_info = $q->fetchRow();
	if (!$last_message_info) { // if it's first response, use original message
		$last_message_info =& $message_info;
		$last_message_info['message_body'] = wordwrap(@$last_message_info['message_body'], 50, "\n> ");
	}
	else {
		$last_message_info['message_body'] = str_replace("\n", "\n> ", @$last_message_info['message_body']);
	}
	$q->clear();
}
?>

<form name="msgforum" action="?m=forums&forum_id=<?=$forum_id?>" method="post">
	<input type="hidden" name="dosql" value="do_post_aed" />
	<input type="hidden" name="del" value="0" />
	<input type="hidden" name="message_forum" value="<?=$forum_id?>" />
	<input type="hidden" name="message_parent" value="<?=$message_parent?>" />
	<input type="hidden" name="message_published" value="<?=$forum_info['forum_moderated'] ? '1' : '0'?>" />
	<input type="hidden" name="message_author" value="<?=(isset($message_info['message_author']) && ($message_id || $message_parent < 0)) ? $message_info['message_author'] : $AppUI->user_id?>" />
	<input type="hidden" name="message_editor" value="<?=(isset($message_info['message_author']) && ($message_id || $message_parent < 0)) ? $AppUI->user_id : '0'?>" />
	<input type="hidden" name="message_id" value="<?=$message_id?>" />

<table cellspacing="0" cellpadding="4" border="0" width="300px">
<tr>
	<th bgcolor="#E0E0E0" colspan="2"><?=$AppUI->_( $message_id ? 'Edit Message' : 'New Message' )?></th>
</tr>
<tr>
	<td align="right"><?=$AppUI->_( 'Subject' )?></td>
	<td>
		<input type="text" name="message_title"
			value="<?=($message_id || $message_parent < 0 ? '' : 'Re: ') .$message_info['message_title']?>"
			size="50" maxlength="250">
	</td>
</tr>
<tr>
	<td align="right" valign="top"><?=$AppUI->_( 'Message' )?></td>
	<td align="left" valign="top">
		<textarea name="message_body" rows="5" style="width: 275px"><?=(($message_id == 0) && ($message_parent != -1)) ?
			'> '.str_replace("\n", "\n> ", $last_message_info['message_body'])."\n" :
			$message_info['message_body']?></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
		<?php
		if ($AppUI->user_id == $message_info['message_author'] ||
			$AppUI->user_id == $forum_info['forum_owner'] ||
			$message_id==0 || (!empty($perms['all']) && !getDenyEdit('all')) ) {
			echo '<input type="button" value="'.$AppUI->_('Send').'" class="button" onclick="doPostMessage()">';
		}
		?>
		<input type="button" value="<?=$AppUI->_('Cancel')?>" class="button" onclick="doCloseDialog()">
	</td>
</tr>
</form>
</table>
